package so.wy.servicecenter.businessbed.service.deposit;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import so.wy.servicecenter.base.exception.ServiceException;
import so.wy.servicecenter.base.frame.baseopt.BaseOptService;
import so.wy.servicecenter.businessbed.dao.rdb.deposit.DepositDAO;
import so.wy.servicecenter.businessbed.dao.rdb.deposit.DepositDO;
import so.wy.servicecenter.data.dao.enums.YesNoEnum;

import java.util.Date;

@Service
@Scope("prototype")
public class DepositService extends BaseOptService<DepositDAO, DepositDO, DepositDTO> {

    @Override
    protected DepositDTO convert2DTO(DepositDO depositDO) {
        return new DepositDTO(depositDO);
    }

    @Override
    protected DepositDO convert2DO(DepositDTO depositQuery) {
        return depositQuery.convert2DO();
    }

    @Override
    public DepositDTO addRecord(DepositDTO recordQuery) throws ServiceException {
        recordQuery.setIsOpen(YesNoEnum.NO.getCode());
        recordQuery.setDepositTime(new Date());
        return super.addRecord(recordQuery);
    }

    //获取定价信息
    public DepositDTO getOpenDeposit() throws ServiceException {
        DepositDO depositDO = getOne(new LambdaQueryWrapper<DepositDO>()
                .eq(DepositDO::getIsOpen, YesNoEnum.YES.getCode()));

        if (depositDO == null){
            throw exception("未设置定价信息");
        }

        return new DepositDTO(depositDO);
    }

    public void toggle(Long id){
        DepositDO depositDO = getById(id);

        YesNoEnum status = YesNoEnum.NO;
        if (YesNoEnum.NO.getCode().equals(depositDO.getIsOpen())){
            status = YesNoEnum.YES;
            DepositDO upDO = new DepositDO();
            upDO.setIsOpen(YesNoEnum.NO.getCode());
            update(upDO,lambdaUpdate().eq(DepositDO::getIsOpen,YesNoEnum.YES.getCode()));
        }

        depositDO.setIsOpen(status.getCode());
        updateById(depositDO);
    }
}
